package com.fanrenqiji.common.utils;

import java.io.File;
import java.util.regex.Matcher;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author lijian E-mail:ljz0721cx@163.com
 * @version 创建时间：2015年3月23日 下午3:17:51
 * @description
 */
public class PathUtils {
	private final static String CLASS_PATH = Thread.currentThread()
			.getContextClassLoader().getResource("").getPath();

	public static String getClassPath() {
		/*String syso = System.getProperty("os.name").toLowerCase();
		// TODO 需要判断解析的系统运行
		if (syso.contains("linux")) {
			CLASS_PATH.replace("/", File.separator);
		}
		if (syso.contains("window")) {
			CLASS_PATH.replace("/", File.separator);
		}*/
		return CLASS_PATH.replace("/", File.separator).trim();
	}
	
	/**
	 * 获取系统访问路径
	 * @return
	 */
	public static String pathAddress() {
		String strResult = "";

		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();

		StringBuffer strBuf = new StringBuffer();

		strBuf.append(request.getScheme() + "://");
		strBuf.append(request.getServerName() + ":");
		strBuf.append(request.getServerPort() + "");

		strBuf.append(request.getContextPath() + "/");

		strResult = strBuf.toString();// +"ss/";//加入项目的名称

		return strResult;
	}

	/**
	 * 通过传入的StringURl获得适应系统的路径
	 * 
	 * @param classPath
	 * @return
	 */
	public static String getRealClassPath(String StringURl) {
		String classpath = StringURl.replace("\\/", File.separator);
		return CLASS_PATH.concat(classpath);
	}

	/**
	 * 通过传入的StringURl获得url路径
	 * 
	 * @param classPath
	 * @return
	 */
	public static String getLocalMapPath(String StringURl) {
		return StringURl.replace("\\/", File.separator);
	}

	public static void main(String[] args) {
		System.out.println(PathUtils.getClassPath());

	}

}
